package com.carbon.assets.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.carbon.assets.entity.*;
import com.carbon.assets.service.CarbonProjectDocService;

import com.carbon.assets.common.BaseController;

import com.carbon.assets.service.CarbonProjectService;
import com.carbon.assets.service.CarbonResourceFileService;
import com.carbon.assets.vo.CarbonCreditAssetsQueryVo;
import com.carbon.assets.vo.SelectorVo;
import com.carbon.domain.common.ApiResult;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import io.swagger.models.auth.In;
import lombok.Data;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * <p>
 * 碳减排项目文档 前端控制器
 * </p>
 *
 * @author Li Jun
 * @since 2022-04-24
 */
@Slf4j
@RestController
@RequestMapping("/carbonProjectDoc")
@Api(value = "碳减排项目文档模块", tags = {"碳减排项目文档模块"})
public class CarbonProjectDocController extends BaseController {
@Resource CarbonProjectDocService carbonProjectDocService;
    @Resource
    CarbonProjectService carbonProjectService;

    @GetMapping("/getList")
    @ApiOperation(value = "获取项目文件列表",notes = "获取项目文件列表")
    public ApiResult<List<CarbonProjectDoc>> getList(@RequestParam String tenantId, @RequestParam String id) {
        LambdaQueryWrapper<CarbonProjectDoc> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(CarbonProjectDoc::getCarbonProjectId,id);
        wrapper.eq(CarbonProjectDoc::getTenantId,tenantId);
        List<CarbonProjectDoc> list = carbonProjectDocService.list(wrapper);
        return ApiResult.ok(list);
    }

    @GetMapping("/queryBySelector")
    public ApiResult queryBySelector(SelectorVo selectorVo) {
        selectorVo.setTenantId(null);
        // 2. 创建分页对象（使用Vo中的分页参数）
        Page<CarbonProjectDoc> page = new Page<>(
                selectorVo.getPageNum(),
                selectorVo.getPageSize()
        );
        // 3. 调用服务层的分页查询方法
        Page<CarbonProjectDoc> resultPage =
                carbonProjectDocService.selectBySelector(page, selectorVo);
        // 4. 构建包含分页信息的返回结果
        return ApiResult.ok(resultPage);
    }

    @GetMapping("/list")
    @ApiOperation("获取项目列表")
    public ApiResult<Map<String, Object>> getProjectList(
            @ApiParam(value = "当前页码", required = true) @RequestParam Integer page,
            @ApiParam(value = "每页条数", required = true) @RequestParam Integer pageSize,
            @ApiParam(value = "项目名称（模糊查询）") @RequestParam(required = false) String projectName,
            @ApiParam(value = "租户ID", required = true) @RequestParam Long tenantId) {

        // 创建分页对象
        Page<CarbonProject> pagination = new Page<>(page, pageSize);

        // 调用服务层查询数据
        IPage<CarbonProject> resultPage = carbonProjectService.queryProjectList(
                pagination, projectName, tenantId);

        // 构建返回结果
        Map<String, Object> data = new HashMap<>();
        data.put("records", resultPage.getRecords());  // 分页数据列表
        data.put("total", resultPage.getTotal());      // 总记录数
        data.put("pages", resultPage.getPages());      // 总页数
        data.put("current", resultPage.getCurrent());  // 当前页码
        data.put("size", resultPage.getSize());        // 每页条数

        return ApiResult.ok(data);
    }

    @GetMapping("/searchFileType")
    @ApiOperation("上传项目前检查前置文件是否存在")
    public ApiResult searchFileType(@RequestParam String projectId,@RequestParam String fileTypeCode){
        System.out.println("---------------------看看进来没----------------------------");
        int code = Integer.parseInt(fileTypeCode);
        LambdaQueryWrapper<CarbonProjectDoc> wr = new LambdaQueryWrapper<>();
        wr.eq(CarbonProjectDoc::getCarbonProjectId,Long.parseLong(projectId));
        if (code>210000001 ){
            String Code = "0" + String.valueOf(code - 1);
            wr.eq((CarbonProjectDoc::getType),Code);
            int count = carbonProjectDocService.count(wr);
            if (count<1){
                if (fileTypeCode.equals("0210000002")){
                    return ApiResult.fail("请先上传设计文档");
                }

                if (fileTypeCode.equals("0210000003")){
                    return ApiResult.fail("请先上传审定报告");
                }
                if (fileTypeCode.equals("0210000004")){
                    return ApiResult.fail("请先上传监测报告");
                }
                if (fileTypeCode.equals("0210000005")){
                    return ApiResult.fail("请先上传核证报告");
                }
            }
        }

        CarbonProject carbonProject = carbonProjectService.getById(projectId);
        carbonProject.setId(Long.parseLong(projectId));
        if (fileTypeCode.equals("0210000001")){
            if (Integer.parseInt(carbonProject.getProjectStatus())<100000003){
                if (Integer.parseInt(carbonProject.getProjectStatus()) == 100000001){
                    return ApiResult.fail("请等待项目立项通过");
                }
            carbonProject.setProjectStatus("0100000003");
            carbonProject.setInitiationDate(new Date());
        }
        }
        if (fileTypeCode.equals("0210000002")){
            if (Integer.parseInt(carbonProject.getProjectStatus())< 100000005 ) {
                if (Integer.parseInt(carbonProject.getProjectStatus())== 100000003){
                    return ApiResult.fail("设计文件还未通过审批，待通过后才可上传");
                }
                carbonProject.setProjectStatus("0100000005");
                carbonProject.setInitiationDate(new Date());
            }
        }

        if (fileTypeCode.equals("0210000003")){
            if (Integer.parseInt(carbonProject.getProjectStatus())<100000008){
                if (Integer.parseInt(carbonProject.getProjectStatus())== 100000005){
                    return ApiResult.fail("审定文件还未通过审批，待通过后才可上传");
                }
            carbonProject.setProjectStatus("0100000008");
            carbonProject.setInitiationDate(new Date());}
        }
        if (fileTypeCode.equals("0210000004")){
            if (Integer.parseInt(carbonProject.getProjectStatus())<100000010) {
                if (Integer.parseInt(carbonProject.getProjectStatus())== 100000008){
                    return ApiResult.fail("监测文件还未通过审批，待通过后才可上传");
                }
                carbonProject.setProjectStatus("0100000010");
                carbonProject.setInitiationDate(new Date());
            }
        }
        if (fileTypeCode.equals("0210000005")){
            if (Integer.parseInt(carbonProject.getProjectStatus())<100000012) {

                if (Integer.parseInt(carbonProject.getProjectStatus())== 100000010){
                    return ApiResult.fail("核证文件还未通过审批，待通过后才可上传");
                }
                carbonProject.setProjectStatus("0100000012");
                carbonProject.setInitiationDate(new Date());
            }
        }
        carbonProjectService.updateById(carbonProject);
        return ApiResult.ok();
    }

    @PostMapping("/add")
    public ApiResult add(@RequestBody CarbonProjectDoc carbonProjectDoc) {
        carbonProjectDoc.setStatus(0);
        Long pid = carbonProjectDoc.getCarbonProjectId();
        CarbonProject project = carbonProjectService.getById(pid);
        project.getProjectStatus();
        carbonProjectDocService.save(carbonProjectDoc);
        return ApiResult.ok("文件创建成功");
    }

    @DeleteMapping("/delete")
    public ApiResult delete(@RequestParam String id) {
        boolean isDeleted = carbonProjectDocService.removeById(id);
        if (isDeleted) {
            return ApiResult.ok("成功删除");
        }else {
            return  ApiResult.fail("没有该条数据");
        }
    }


    @PostMapping("/add004")
    public ApiResult add004(@RequestParam String id,@RequestParam String carbonProjectId) {
        CarbonProject carbonProject = new CarbonProject();
        carbonProject.setId(Long.parseLong(carbonProjectId));
        String projectStatus = carbonProjectService.getById(Long.parseLong(carbonProjectId)).getProjectStatus();
        if (Integer.parseInt(projectStatus)< 100000004){
            if (Integer.parseInt(projectStatus) <= 100000001){
                return ApiResult.fail("项目还未通过立项");
            }
            carbonProject.setProjectStatus("0100000004");
            carbonProjectService.updateById(carbonProject);
        }

        CarbonProjectDoc carbonProjectDoc = new CarbonProjectDoc();
        carbonProjectDoc.setId(Long.parseLong(id));
        carbonProjectDoc.setStatus(1);
        carbonProjectDoc.setCompletionDate(new Date());
        carbonProjectDocService.updateById(carbonProjectDoc);
        return ApiResult.ok("审批成功");
    }

    @PostMapping("/add005")
    public ApiResult add005(@RequestParam String id,@RequestParam String carbonProjectId) {
        CarbonProject carbonProject = new CarbonProject();
        carbonProject.setId(Long.parseLong(carbonProjectId));
        String projectStatus = carbonProjectService.getById(Long.parseLong(carbonProjectId)).getProjectStatus();
        if (Integer.parseInt(projectStatus)< 100000006){
            if (Integer.parseInt(projectStatus) <= 100000003){
                return ApiResult.fail("请先审批审定文档");
            }
            carbonProject.setProjectStatus("0100000006");
            carbonProject.setApprovalDate(new Date());
            carbonProjectService.updateById(carbonProject);
            carbonProjectService.updateById(carbonProject);
        }

        CarbonProjectDoc carbonProjectDoc = new CarbonProjectDoc();
        carbonProjectDoc.setId(Long.parseLong(id));
        carbonProjectDoc.setStatus(1);
        carbonProjectDoc.setCompletionDate(new Date());
        carbonProjectDocService.updateById(carbonProjectDoc);
        return ApiResult.ok("审批成功");
    }

    @PostMapping("/add009")
    public ApiResult add009(@RequestParam String id,@RequestParam String carbonProjectId) {
        CarbonProject carbonProject = new CarbonProject();
        carbonProject.setId(Long.parseLong(carbonProjectId));
        String projectStatus = carbonProjectService.getById(Long.parseLong(carbonProjectId)).getProjectStatus();
        if (Integer.parseInt(projectStatus)< 100000009){
            if (Integer.parseInt(projectStatus) <= 100000005){
                return ApiResult.fail("请先通过审定文档");
            }
            carbonProject.setProjectStatus("0100000009");
            carbonProjectService.updateById(carbonProject);
            carbonProject.setRecordFilingDate(new Date());
            carbonProjectService.updateById(carbonProject);
        }

        CarbonProjectDoc carbonProjectDoc = new CarbonProjectDoc();
        carbonProjectDoc.setId(Long.parseLong(id));
        carbonProjectDoc.setStatus(1);
        carbonProjectDoc.setCompletionDate(new Date());
        carbonProjectDocService.updateById(carbonProjectDoc);
        return ApiResult.ok("审批成功");
    }

    @PostMapping("/add011")
    public ApiResult add011(@RequestParam String id,@RequestParam String carbonProjectId) {
        CarbonProject carbonProject = new CarbonProject();
        carbonProject.setId(Long.parseLong(carbonProjectId));
        String projectStatus = carbonProjectService.getById(Long.parseLong(carbonProjectId)).getProjectStatus();
        if (Integer.parseInt(projectStatus)< 100000011){
            if (Integer.parseInt(projectStatus) <= 100000008){
                return ApiResult.fail("请先审批监测文档");
            }
            carbonProject.setProjectStatus("0100000011");
            carbonProjectService.updateById(carbonProject);
        }
        carbonProject.setCertifiedDate(new Date());
        carbonProjectService.updateById(carbonProject);
        CarbonProjectDoc carbonProjectDoc = new CarbonProjectDoc();
        carbonProjectDoc.setId(Long.parseLong(id));
        carbonProjectDoc.setStatus(1);
        carbonProjectDoc.setCompletionDate(new Date());
        carbonProjectDocService.updateById(carbonProjectDoc);
        return ApiResult.ok("审批成功");
    }

    @PostMapping("/add013")
    public ApiResult add013(@RequestParam String id,@RequestParam String carbonProjectId) {
        CarbonProject carbonProject = new CarbonProject();
        carbonProject.setId(Long.parseLong(carbonProjectId));
        String projectStatus = carbonProjectService.getById(Long.parseLong(carbonProjectId)).getProjectStatus();
        if (Integer.parseInt(projectStatus)< 100000013){
            if (Integer.parseInt(projectStatus) <= 100000010){
                return ApiResult.fail("请先通过核证文档");
            }
            carbonProject.setProjectStatus("0100000013");
            carbonProject.setIssuingDate(new Date());
            carbonProjectService.updateById(carbonProject);
        }
        CarbonProjectDoc carbonProjectDoc = new CarbonProjectDoc();
        carbonProjectDoc.setId(Long.parseLong(id));
        carbonProjectDoc.setStatus(1);
        carbonProjectDoc.setCompletionDate(new Date());
        carbonProjectDocService.updateById(carbonProjectDoc);


        return ApiResult.ok("审批成功");
    }
}

